﻿using Face8280WebSocketServer.DB.Log;
using Face8280WebSocketServer.DTO.Log;
using Face8280WebSocketServer.Interface;
using Face8280WebSocketServer.Utility.FilterAttribute;
using Face8280WebSocketServer.Utility.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;

namespace FaceFace8280WebSocketServer.Controllers.New.Log
{
    [Route("api/[controller]")]
    [ApiController]
    public class FaceIOLogController : ControllerBase
    {
        IFaceIOLogService _FaceIOLogService;
        public FaceIOLogController(IFaceIOLogService faceIOLogService)
        {
            _FaceIOLogService = faceIOLogService;
        }
        /// <summary>
        /// 获取日志表
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("GetLogs")]
        [Authorize]
        [TypeFilter(typeof(VerifyActionFilterAttribute))]
        public IActionResult GetReport([FromForm] FaceIOLogQueryParameter par)
        {
            List<Expression<Func<FaceIOLog, bool>>> oWheres = new List<Expression<Func<FaceIOLog, bool>>>();
            oWheres.Add(x => x.CreateTime >= par.LogTimeBegin && x.CreateTime <= par.LogTimeEnd);

            if (!string.IsNullOrWhiteSpace(par.DeviceSN)) oWheres.Add(x => x.DeviceSN.Contains(par.DeviceSN));
            if (!string.IsNullOrWhiteSpace(par.Method)) oWheres.Add(x => x.Method.Contains(par.Method));
            if (!string.IsNullOrWhiteSpace(par.IpAddr)) oWheres.Add(x => x.IPAddr.Contains(par.IpAddr));
            if (!string.IsNullOrWhiteSpace(par.Body)) oWheres.Add(x => x.Body.Contains(par.Body));


            //_logger.LogInformation("获取通讯日志列表");
            var reports = _FaceIOLogService.QueryPage(
                oWheres, par.pageSize, par.pageIndex,
                x => x.CreateTime,
                par.isAsc);

            return new JsonResult(new JsonResultModel(reports));
        }

        /// <summary>
        /// 删除日志
        /// </summary>
        /// <param name="par"></param>
        /// <returns></returns>
        [HttpDelete]
        [Route("DeleteLogs")]
        [Authorize]
        public IActionResult DeleteLogs()
        {
            //_logger.LogInformation("清空通讯日志列表");
            _FaceIOLogService.ClearLog();
            return new JsonResult(new JsonResultModel());
        }
    }
}
